/*
 * @Author: zhengzhuang
 * @since: 2020-01-08 12:00:32
 * @lastTime: 2020-03-01 14:43:05
 * @LastAuthor: zhengzhuang
 * @FilePath: /C20Driver/lib/pages/carpool_page/generate_windmill_trip.dart
 * @message: 顺风车行程已发布 正在寻找顺路乘客... GenerateWindmillTrip
 */

import 'dart:convert';

import 'package:C20Driver/config/service_api.dart';
import 'package:C20Driver/model/carpool_passenger_order_info.dart';
import 'package:C20Driver/provider/order_information.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/service/service_method.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:async/async.dart';

class GenerateWindmillTrip extends StatefulWidget {
  final String id;
  final String start;
  GenerateWindmillTrip({this.id, this.start});
  @override
  _GenerateWindmillTripState createState() => _GenerateWindmillTripState();
}

class _GenerateWindmillTripState extends State<GenerateWindmillTrip> {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  bool _updata = true;

  @override
  void initState() {
    super.initState();
    // _getFellowList();
  }

  Future _getFellowList() async {
    return await DioUtils().request(
        ReqMethod.POST, ServiceApi.getUserRideOrderList,
        params: {'ride_id': widget.id, 'page': '1', 'pageSize': '100'},
        success: (res) async {
      print('11111111====' + res);
      List list = json.decode(res);
      return list;
    });
  }

  @override
  Widget build(BuildContext context) {
    OrderInformation provider =
        Provider.of<OrderInformation>(context, listen: true);
    return Scaffold(
      appBar: AppBar(
        title: Text(''),
        elevation: 0,
      ),
      body: Container(
        color: Color(0xffffffff),
        padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(32),
          ScreenUtil().setWidth(36),
          ScreenUtil().setWidth(32),
          ScreenUtil().setWidth(10),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              widget.start == '0000' ? '正在寻找顺路乘客...' : '可拼乘客',
              style: TextStyle(
                  color: Color(0xff333333),
                  fontSize: ScreenUtil().setWidth(36),
                  fontWeight: FontWeight.w500),
            ),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(
                    right: ScreenUtil().setWidth(30),
                  ),
                  width: ScreenUtil().setWidth(15.0),
                  height: ScreenUtil().setWidth(15.0),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color(0xff00BA8F),
                  ),
                ),
                Expanded(
                  child: Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '${provider.carpoolOrderInfo.depAre}',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(34.0),
                        color: Color(0xff333333),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setWidth(32.0)),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(
                    left: ScreenUtil().setWidth(0),
                    right: ScreenUtil().setWidth(30),
                  ),
                  width: ScreenUtil().setWidth(15.0),
                  height: ScreenUtil().setWidth(15.0),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color.fromARGB(255, 255, 102, 0),
                  ),
                ),
                Expanded(
                  child: Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '${provider.carpoolOrderInfo.destArea}',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(34.0),
                        color: Color(0xff333333),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setWidth(40.0)),
            Container(
              width: double.infinity,
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil().setWidth(32),
                  ScreenUtil().setWidth(26),
                  ScreenUtil().setWidth(32),
                  ScreenUtil().setWidth(26)),
              decoration: BoxDecoration(
                color: Color(0xffF6FAF6),
                borderRadius: BorderRadius.all(
                  Radius.circular(
                    ScreenUtil().setWidth(20),
                  ),
                ),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Text(
                    '${provider.carpoolOrderInfo.departureDate}',
                    style: TextStyle(
                        color: Color(0xff668D6E),
                        fontSize: ScreenUtil().setWidth(28),
                        fontWeight: FontWeight.w500),
                  ),
                  Container(
                    width: ScreenUtil().setWidth(3),
                    height: ScreenUtil().setWidth(36),
                    color: Color(0xffDEECE0),
                  ),
                  Text(
                    '${provider.carpoolOrderInfo.seatNum}座',
                    style: TextStyle(
                        color: Color(0xff668D6E),
                        fontSize: ScreenUtil().setWidth(28),
                        fontWeight: FontWeight.w500),
                  ),
                ],
              ),
            ),
            SizedBox(height: ScreenUtil().setWidth(40.0)),
            Container(
              width: double.infinity,
              height: ScreenUtil().setWidth(100.0),
              decoration: BoxDecoration(
                  color: widget.start == '0000'
                      ? Color(0x223FB956)
                      : Color(0xff3FB956),
                  borderRadius: BorderRadius.all(
                      Radius.circular(ScreenUtil().setWidth(50)))),
              child: MaterialButton(
                textColor: widget.start == '0000'
                    ? Color(0xff3FB956)
                    : Color(0xffffffff),
                child: Text(
                  widget.start == '0000' ? '取消发布' : '查看乘客',
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(32),
                      fontWeight: FontWeight.w500),
                ),
                onPressed: () {
                  if (widget.start == '0000') {
                    _cancelFreeRideOrder();
                  } else {
                    //查看乘客
                    NavigatorUtil.goBack(context);
                  }
                },
              ),
            ),
            SizedBox(height: ScreenUtil().setWidth(50.0)),
            Text(
              '为您推荐以下乘客',
              style: TextStyle(
                  color: Color(0xff999999),
                  fontSize: ScreenUtil().setWidth(28),
                  fontWeight: FontWeight.w400),
            ),
            SizedBox(height: ScreenUtil().setWidth(30.0)),
            Expanded(
              child: Container(
                // height: ScreenUtil().setWidth(920),
                child: RefreshIndicator(
                  onRefresh: () async {
                    setState(() {
                      _updata = true;
                    });
                  },
                  child: FutureBuilder(
                    future: _updata ? _getFellowList() : null,
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.hasData) {
                        List dataSnap = snapshot.data;
                        return ListView.builder(
                          itemCount: dataSnap.length,
                          itemBuilder: (BuildContext context, index) {
                            return _getListViewCell(dataSnap[index]);
                          },
                        );
                      } else {
                        return Container();
                      }
                    },
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _getListViewCell(Map data) {
    Map user = data;
    return InkWell(
      onTap: () {
        CarpoolPassengerOrderInfo data =
            CarpoolPassengerOrderInfo.fromJson(user);
        OrderInformation provider =
            Provider.of<OrderInformation>(context, listen: false);
        provider.setCarpoolPassengerOrderInfo(data);
        print(provider.carpoolPassengerOrderInfo.orderId);
        NavigatorUtil.jump(
            context, '/carpoolPassengerMap?type=0000&first=${widget.start}');
      },
      child: Container(
        width: double.infinity,
        margin: EdgeInsets.only(
            top: ScreenUtil().setWidth(30),
            left: ScreenUtil().setWidth(2),
            right: ScreenUtil().setWidth(2)),
        padding: EdgeInsets.all(ScreenUtil().setWidth(32)),
        decoration: BoxDecoration(
            color: Color(0xffffffff),
            borderRadius: BorderRadius.all(
              Radius.circular(
                ScreenUtil().setWidth(30),
              ),
            ),
            boxShadow: <BoxShadow>[
              BoxShadow(
                color: Colors.black12,
                blurRadius: ScreenUtil().setSp(12.0),
              ),
            ]),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
                Text(
                  data['departure_date'],
                  style: TextStyle(
                      color: Color(0xff333333),
                      fontSize: ScreenUtil().setWidth(32),
                      fontWeight: FontWeight.w500),
                ),
                RichText(
                  text: TextSpan(
                    text: data['road_level'] + '%',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(36.0),
                      color: Color(0xff3FB956),
                      fontWeight: FontWeight.w500,
                    ),
                    children: <TextSpan>[
                      TextSpan(
                        text: '顺路',
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(28.0),
                          color: Color(0xff3FB956),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(
              height: ScreenUtil().setWidth(20),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(
                      ScreenUtil().setWidth(20),
                      ScreenUtil().setWidth(8),
                      ScreenUtil().setWidth(20),
                      ScreenUtil().setWidth(8)),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(0xffF6FAF6),
                    borderRadius: BorderRadius.all(
                      Radius.circular(
                        ScreenUtil().setWidth(10),
                      ),
                    ),
                  ),
                  child: Text(
                    '${data['people_number']}人',
                    style: TextStyle(
                        color: Color(0xff668D6E),
                        fontSize: ScreenUtil().setWidth(26),
                        fontWeight: FontWeight.w400),
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(44)),
                Container(
                  padding: EdgeInsets.fromLTRB(
                      ScreenUtil().setWidth(20),
                      ScreenUtil().setWidth(8),
                      ScreenUtil().setWidth(20),
                      ScreenUtil().setWidth(8)),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(0xffF6FAF6),
                    borderRadius: BorderRadius.all(
                      Radius.circular(
                        ScreenUtil().setWidth(10),
                      ),
                    ),
                  ),
                  child: Text(
                    data['ride_type'] == '0010' ? '拼车' : '独享',
                    style: TextStyle(
                        color: Color(0xff668D6E),
                        fontSize: ScreenUtil().setWidth(26),
                        fontWeight: FontWeight.w400),
                  ),
                ),
              ],
            ),
            SizedBox(
              height: ScreenUtil().setWidth(40),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Container(
                  width: ScreenUtil().setWidth(450),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.only(
                              right: ScreenUtil().setWidth(30),
                            ),
                            width: ScreenUtil().setWidth(15.0),
                            height: ScreenUtil().setWidth(15.0),
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              color: Color(0xff00BA8F),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                data['dep_are'] != null ? data['dep_are'] : '',
                                style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30.0),
                                  color: Color(0xff333333),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: ScreenUtil().setWidth(28.0)),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(0),
                              right: ScreenUtil().setWidth(30),
                            ),
                            width: ScreenUtil().setWidth(15.0),
                            height: ScreenUtil().setWidth(15.0),
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              color: Color.fromARGB(255, 255, 102, 0),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                data['dest_area'],
                                style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30.0),
                                  color: Color(0xff333333),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                RichText(
                  text: TextSpan(
                    text: '￥',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(28.0),
                      color: Color(0xff333333),
                    ),
                    children: <TextSpan>[
                      TextSpan(
                        text: '${data['total_price']}',
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(32.0),
                            color: Color(0xff333333),
                            fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(
              height: ScreenUtil().setWidth(30),
            ),
            Divider(
              color: Color(0xffeeeeee),
              height: ScreenUtil().setWidth(1),
            ),
            SizedBox(
              height: ScreenUtil().setWidth(30),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                ClipOval(
                  child: Container(
                    width: ScreenUtil().setWidth(56.0),
                    height: ScreenUtil().setWidth(56.0),
                    child: Image.network(data['headimgurl']),
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(12.0)),
                Text(
                  '${data['passenger_name']}',
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(28.0),
                    color: Color(0xff333333),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  /// 取消行程
  _cancelFreeRideOrder() {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('行程取消'),
          content: Text('您确认要取消当前行程?'),
          actions: <Widget>[
            FlatButton(
              child: Text('取消行程'),
              onPressed: () async {
                Navigator.of(context).pop();
                bool back = await Request.cancelFreeRideOrder(
                    context: context, id: widget.id);
                if (back) {
                  Provider.of<OrderInformation>(context, listen: false)
                      .cleanCarpoolLocalInfo();
                  NavigatorUtil.goBack(context);
                }
              },
            ),
            FlatButton(
              child: Text('再想想'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }

  @override
  void dispose() {
    super.dispose();
  }
}
